from lxml import etree

def lingya_index_parse(html_str: str):
    """
    解析网页核心逻辑:
        1. 先用xpath将页面内的所有茶叶信息提取出来
        2. 将前7类茶叶的信息添加到列表中
        3. 将后2类茶叶信息添加到列表
    :param html_str: 前端网页代码
    :return:
    """
    etree_html = etree.HTML(html_str)
    div_list = etree_html.xpath('//div[@class="main-top"]/div')
    caa_yees = add_caayee_7(div_list)
    caa_yees.append([div_list[35], div_list[36]])
    caa_yees.append([div_list[37], div_list[38]])
    return caa_yees


def add_caayee_7(div_list):
    """
    将7种有性能介绍的茶叶添加到一个列表中，每种茶叶包含5部分数据
    1. 茶叶名
    2. 特征
    3. 功效
    4. 适宜人群
    5. 包含的茶叶品种
    :param div_list: 茶叶element列表
    :return: 结构化的茶叶列表
    """
    print('列表长度为：', len(div_list))
    caa_yees = []
    for i in range(1, 8):
        print('第{}种茶叶数据开始添加到列表中:'.format(i))
        caa_yee = []
        for j in range(0, 5):
            i_j = (i - 1) * 5 + j
            caa_yee.append(div_list[i_j])
        caa_yees.append(caa_yee)
    return caa_yees


def get_contex(html: str):
    caa_yees = lingya_index_parse(html)
    # 将前7类茶叶性质及茶叶名提取出来
    for i in range(7):
        caa_yee_class_name = caa_yees[i][0].xpath('./text()')[1].strip()
        caa_yee_class_features = caa_yees[i][1].xpath('./text()')[0].strip()
        caa_yee_class_effect = caa_yees[i][2].xpath('./text()')[0].strip()
        caa_yee_class_suitable = caa_yees[i][3].xpath('./text()')[0].strip()
        caa_yee_class_children = caa_yees[i][4].xpath('./a')

        print(caa_yee_class_name)
        print(caa_yee_class_features)
        print(caa_yee_class_effect)
        print(caa_yee_class_suitable)

        # 将每类茶叶的子类信息提取出来
        for child in caa_yee_class_children:

            caa_yee_name = child.xpath('./div[@class="g-c-5 g-m5-b"]/text()')[0]
            caa_yee_local_province = child.xpath('./div[@class="bk-tag"]/text()')[0].split(' ')[0]
            try:
                caa_yee_local_city = child.xpath('./div[@class="bk-tag"]/text()')[0].split(' ')[1]
            except:
                caa_yee_local_city = child.xpath('./div[@class="bk-tag"]/text()')[0].split(' ')[0]
            caa_yee_img = child.xpath('./img/@data-src')[0]

            print(caa_yee_name)
            print(caa_yee_local_province, caa_yee_local_city)
            print(caa_yee_img)


# 3.保存的index页进行解析，提取想要的内容
with open('lingya_index.html', 'r', encoding='utf-8') as html_file:
    html = html_file.read()
    get_contex(html)
